/*
 * MIT License
 *
 * Copyright (c) 2019 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.commons.file;

import cn.seaboot.commons.core.Converter;
import cn.seaboot.commons.lang.P;
import cn.seaboot.commons.lang.Symbol;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * Properties 加载工具
 *
 * @author Mr.css on 2017-8-21
 */
public class PropertiesUtils {
    /**
     * 默认换行符
     */
    private static final byte[] lineSeparatorBytes = P.lineSeparator().getBytes();

    private PropertiesUtils() {
    }

    /**
     * 将Map内的信息写到Prop
     *
     * @param map    -
     * @param os     -
     * @param append -
     * @throws IOException -
     */
    public static void writeMap(Map<?, ?> map, Writer os, boolean append) throws IOException {
        for (Entry<?, ?> entry : map.entrySet()) {
            os.write(Converter.toString(entry.getKey()));
            os.write(" = ");
            os.write(Converter.toString(entry.getValue()));
            os.write(P.lineSeparator());
        }
    }

    /**
     * 将 map 内的信息写到 properties 文件中
     *
     * @param map    -
     * @param os     -
     * @param append -
     * @throws IOException -
     */
    public static void writeMap(Map<?, ?> map, OutputStream os, boolean append) throws IOException {
        for (Entry<?, ?> entry : map.entrySet()) {
            os.write(Converter.toString(entry.getKey()).getBytes());
            os.write(Symbol.SPACE);
            os.write(Symbol.EQUAL);
            os.write(Symbol.SPACE);
            os.write(Converter.toString(entry.getValue()).getBytes());
            os.write(lineSeparatorBytes);
        }
    }

    /**
     * 载入配置信息
     *
     * @param inputStream is
     * @return prop
     * @throws IOException -
     */
    public static Properties load(InputStream inputStream) throws IOException {
        Properties prop = new Properties();
        prop.load(inputStream);
        return prop;
    }
}
